package ynu.lcy.service;


import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.lcy.dao.IOrderDao;
import ynu.lcy.dto.order.AddOrderRequestDTO;
import ynu.lcy.elememicroservicecommon.constant.CommonConstant;

import ynu.lcy.elememicroservicecommon.exception.BusinessException;
import ynu.lcy.elememicroservicecommon.utils.ErrorCode;
import ynu.lcy.elememicroserviceserviceclient.service.AuthFeignClient;
import ynu.lcy.elememicroserviceserviceclient.service.CartFeignClient;

import ynu.lcy.entity.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static ynu.lcy.elememicroservicecommon.constant.CommonConstant.ACCESS_TOKEN;
import static ynu.lcy.elememicroservicecommon.utils.TokenUtils.getToken;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private IOrderDao dao;

    @Resource
    private AuthFeignClient authFeignClient;
    @Resource
    private CartFeignClient cartFeignClient;

    /**
     * 创建订单
     *
     * @param dto     订单信息
     * @param request 请求
     */
    @Transactional
    public Order create(AddOrderRequestDTO dto, HttpServletRequest request) {
        Long loginUser = authFeignClient.getLoginUser(getToken(request));
        //获取购物车内容
        Long cartId = dto.getCartId();
        Cart cart = null;
        if (cartId <= 0) return null;
        cart = cartFeignClient.get(cartId);
        //添加订单细则
        List<OrderDetails> orderDetailsList = new ArrayList<>();
        cart.getCartItems().forEach(cartFoodItem -> {
            Integer quantity = cartFoodItem.getQuantity();
            if (quantity>0){
                Food food = cartFoodItem.getFood();
                OrderDetails orderDetails = new OrderDetails().create(quantity, food);
                orderDetailsList.add(orderDetails);
            }
        });
        //创建订单
        Order order = new Order().create(dto.getBusinessId(), loginUser, dto.getAddressId(), orderDetailsList);
        return dao.save(order);
    }

    /**
     * 订单支付
     *
     * @param orderId 订单ID
     * @return 订单
     */
    public Order payOrder(Long orderId) {
        Order order = dao.getReferenceById(orderId);
        order.setOrderState(CommonConstant.ORDER_STATE_PAID);
        return dao.save(order);
    }

    /**
     * 获取订单
     * @param orderId 订单编号
     *
     * @return 订单
     * */
    public Order getOrderByOrderId(Long orderId) {
        return  dao.getReferenceById(orderId);
    }

    /**
     * 获取全部订单
     * @param request 请求
     * @return 订单列表
     * */
    public List<Order> list(HttpServletRequest request){
        Long loginUser = authFeignClient.getLoginUser(getToken(request));
        return dao.findAllByCustomerId(loginUser);
    }

    /**
     * 获取商家订单
     * @param businessId businessId
     * @return 订单列表
     * */
    public List<Order> list(Long businessId){
        return dao.findAllByBusinessId(businessId);
    }

}
